package awesome.request.processor;

import java.io.IOException;

import awesome.request.Request;
import awesome.request.RequestQueue;
import awesome.site.proprietor.SiteProprietor;

/**
 * Processes requests from RequestQueue.
 * 
 * @author Chris Carpenter
 * 
 */
public class RequestProcessor implements Runnable {
	
	private RequestQueue requestQueue;
	private HttpRequestProcessor httpProcessor;
	
	
	public RequestProcessor(RequestQueue _requestQueue, SiteProprietor _siteProprietor) {
		this.requestQueue = _requestQueue;
		this.httpProcessor = new HttpRequestProcessor(_siteProprietor);
	}
	
	@Override
	public void run() {
		
		Request nextRequest;
		while (!Thread.currentThread().isInterrupted()) {
			try {
				nextRequest=requestQueue.take(); //blocks until available
				httpProcessor.processRequest(nextRequest);
			} catch (InterruptedException e) {
				return; //expected, close
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
